/***************************************************************************
 * Copyright 2012-2013 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the contract ICT-257367.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.core.util.parser;

import it.txt.ens.core.ENSResource;
import it.txt.ens.core.factory.ENSResourceFactory;
import it.txt.ens.core.factory.URIBuildingException;
import it.txt.ens.core.factory.URIParseException;

import java.net.URI;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;

/**
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com)
 * @author Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com)
 *
 */
public class ENSResourceURIParser {
    //TODO DELETE
//    private static final Logger LOGGER = Logger.getLogger(ENSResourceURIParser.class.getName());
    private static final ResourceBundle MESSAGES = ResourceBundle.getBundle(
            "logging-messages/" + ENSResourceURIParser.class.getSimpleName(),
            Locale.ROOT);
    
    
    private static final int NUMBER_OF_QUERY_PARAMETERS = 2;
    
    public static ENSResource parse (URI ensResourceURI, ENSResourceFactory factory) throws URIParseException, IllegalArgumentException{
//        LOGGER.fine("1.1");
        URIBuilder builder = new URIBuilder(ensResourceURI);
//        LOGGER.fine("1.2");
        String scheme = builder.getScheme();
//        LOGGER.fine("1.3");
        if (ENSResource.URI_SCHEME.equals(scheme)) {
//            LOGGER.fine("1.4");
            List<NameValuePair> parameters = builder.getQueryParams();
//            LOGGER.fine("1.5");
            if (parameters.size() == NUMBER_OF_QUERY_PARAMETERS) {
//                LOGGER.fine("1.6");
                String namespace;
                String pattern;
                NameValuePair param1 = parameters.get(0);
//                LOGGER.fine("1.7");
                NameValuePair param2 = parameters.get(1);
//                LOGGER.fine("1.8");
                if (ENSResource.NAMESPACE_PARAMETER_NAME.equals(param1.getName())) {
//                    LOGGER.fine("1.8.1.1");
                    namespace = param1.getValue();
//                    LOGGER.fine("1.8.1.2");
                } else if (ENSResource.NAMESPACE_PARAMETER_NAME.equals(param2.getName())) {
//                    LOGGER.fine("1.8.2.1");
                    namespace = param2.getValue();
//                    LOGGER.fine("1.8.2.2");
                } else
                    throw new URIParseException(MessageFormat.format(MESSAGES.getString(
                            "URIParser.parse.missingQueryParameter"), ENSResource.NAMESPACE_PARAMETER_NAME));
//                LOGGER.fine("1.9");
                if (ENSResource.PATTERN_PARAMETER_NAME.equals(param1.getName())) {
//                    LOGGER.fine("1.9.1.1");
                    pattern = param1.getValue();
//                    LOGGER.fine("1.9.1.2");
                } else if (ENSResource.PATTERN_PARAMETER_NAME.equals(param2.getName())) {
//                    LOGGER.fine("1.9.2.1");
                    pattern = param2.getValue();
//                    LOGGER.fine("1.9.2.2");
                } else
                    throw new URIParseException(MessageFormat.format(MESSAGES.getString(
                            "URIParser.parse.missingQueryParameter"), ENSResource.PATTERN_PARAMETER_NAME));
                try {
//                    LOGGER.fine("1.10");
                    builder.getHost();
//                    LOGGER.fine("1.11");
                    builder.getPath();
//                    LOGGER.fine("1.12");
                    return factory.create(builder.getHost(), builder.getPath(), namespace, pattern);
                } catch (URIBuildingException e) {
                    throw new URIParseException("An unexpected exception occurred while creating the ENS resource", e);
                }
            } else
                throw new URIParseException(MessageFormat.format(MESSAGES.getString(
                        "URIParser.parse.unexpectedNumberOfQueryParams"), parameters.size(), 
                    NUMBER_OF_QUERY_PARAMETERS));
        } else
            throw new URIParseException(MessageFormat.format(
                MESSAGES.getString("URIParser.parse.unexpectedScheme"),scheme,ENSResource.URI_SCHEME));
    }
}
